﻿@page "/GridSelectionMultiple"
@using Zt.QmtqDemo.Web.Model
@using Zt.QmtqDemo.Web.Services
@using Zt.QmtqDemo.ConfmPlan
@using System.ComponentModel.DataAnnotations
@using System.Threading
@using System.Threading.Tasks
@using System.Net.Http
@using System.Text.Json
@using System.Linq;
@using DevExtreme.AspNet.Data
@inject FlatProductService FlatProductService
@inject IConfmPlanAppService ConfmPlanAppService

<div class="card demo-card-wide border-bottom-0" style="border-bottom-left-radius: 0; border-bottom-right-radius: 0">

    <EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit" Context="EditFormContext">
        <DataAnnotationsValidator />
        <DxFormLayout>
            <DxFormLayoutItem Caption="产别:" ColSpanMd="3">
                <Template>
                    <DxComboBox NullText="选择产别 ..."
                                ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
                                @bind-SelectedItem="@starship.FacNo"
                                Data="@(new List<string>() { "Zt", "Ms", "bg" })" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="合同号:" ColSpanMd="3">
                <Template>
                    <DxTextBox @bind-Text="@starship.OrderNo" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="库号:" ColSpanMd="3">
                <Template>
                    <DxComboBox NullText="选择库号 ..."
                                ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
                                @bind-SelectedItem="@starship.StockNo"
                                Data="@(new List<string>() { "常州", "南通", "苏州" })" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="材料号:" ColSpanMd="3">
                <Template>
                    <DxTextBox @bind-Text="@starship.MatNo" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="机组代码:" ColSpanMd="3">
                <Template>
                    <DxTextBox @bind-Text="@starship.UNIT_CODE" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="类别:" ColSpanMd="3">
                <Template>
                    <DxComboBox NullText="选择类别 ..."
                                ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
                                @bind-SelectedItem="@starship.SG_SIGN"
                                Data="@(new List<string>() { "AA", "BB", "CC" })" />

                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="交货期:" ColSpanMd="6">
                <Template>
                    <DxDateEdit @bind-Date="@starship.DeliverDate1" /> -
                    <DxDateEdit @bind-Date="@starship.DeliverDate2" />
                </Template>
            </DxFormLayoutItem>
            <DxFormLayoutItem ColSpanMd="12">
                <Template>

                </Template>
            </DxFormLayoutItem>
        </DxFormLayout>
    </EditForm>
</div>


@code {
    private Tmrbr001 starship = new Tmrbr001() { DeliverDate1 = DateTime.Now.AddDays(-30), DeliverDate2 = DateTime.Now + TimeSpan.FromDays(1) };

    private void HandleValidSubmit()
    {
        Console.WriteLine("OnValidSubmit");
    }
    private void HandleInvalidSubmit()
    {
        Console.WriteLine("OnInvalidSubmit");
    }

    public class Tmrbr001
    {
        [Required(ErrorMessage = "必须指定产别")]
        [StringLength(16,
        ErrorMessage = "最多16个字符")]
        public string FacNo { get; set; }

        [Required(ErrorMessage = "合同号")]
        public string OrderNo { get; set; }

        public string StockNo { get; set; }

        public string MatNo { get; set; }

        public DateTime DeliverDate2 { get; set; }

        public DateTime DeliverDate1 { get; set; }

        public string UNIT_CODE { get; set; }

        public string SG_SIGN { get; set; }
    }
}

@if (DataSource == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <DxFormLayout>
        <div class="card demo-card-wide border-bottom-0" style="border-bottom-left-radius: 0; border-bottom-right-radius: 0">
            <div class="card-header border-bottom-0">The total count of selected rows: <b>@SelectedCount</b> (stored selected rows: <b>@StoredSelectedCount</b>, stored unselected rows: <b>@StoredUnselectedCount</b>)</div>
        </div>

        <DxDataGrid Data="@DataSource" @ref="@grid"
                    CssClass="datagrid-border-radius-top-0"
                    KeyFieldName="Id"
                    SelectionMode="DataGridSelectionMode.OptimizedMultipleSelection"
                    SelectAllMode="DataGridSelectAllMode.AllPages"
                    ShowFilterRow="false"
                    OptimizedMultipleSelectionChanged="OnSelectionChanged"
                    ShowPager="true">
            <DxDataGridSelectionColumn Width="50px"></DxDataGridSelectionColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.StockNo)" Caption="库号"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.MatNo)" Caption="材料号"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.OrderNo)" Caption="合同号"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.MatStatus)" Caption="材态"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.MatThick)" Caption="厚度"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.MatWidth)" Caption="宽度"></DxDataGridColumn>
            <DxDataGridColumn Field="@nameof(Tmrbr001ConfirmPlanResultDto.MatLen)" Caption="长度"></DxDataGridColumn>
        </DxDataGrid>
    </DxFormLayout>

}

<DxFormLayout>
    <DxFormLayoutItem ColSpanMd="12">
        <Template>
            <div class="card demo-card-wide border-bottom-0 col-12 col-sm-12" style="border-bottom-left-radius: 0; border-bottom-right-radius: 0">

                <div class="row col-12 col-sm-12" style="display: flex; flex-wrap: wrap; align-items: baseline; justify-content: flex-start">
                    <div class="col col-6 col-sm-auto" style="border: 0px; padding: 1em 0.5em;">
                        <DxButton SubmitFormOnClick="true" Text="待准发查询" RenderStyle="@ButtonRenderStyle.Secondary" Click="@Query" />
                    </div>
                    <div class="col col-6 col-sm-auto" style="border: 0px; padding: 1em 0.5em;">
                        <DxButton Text="收池" RenderStyle="@ButtonRenderStyle.Secondary" Click="@CreatePopl001" />
                    </div>
                    <div class="col col-6 col-sm-auto" style="border: 0px; padding: 1em 0.5em;">
                        <DxButton Text="跳转编辑" RenderStyle="@ButtonRenderStyle.Secondary" />
                    </div>
                </div>
            </div>
        </Template>
    </DxFormLayoutItem>
</DxFormLayout>
<DxFormLayout>
    <div class="card demo-card-wide border-bottom-0" style="border-bottom-left-radius: 0; border-bottom-right-radius: 0">
        <div class="card-header border-bottom-0">信息提示: <b>@Message</b>)</div>
    </div>
</DxFormLayout>
    @code {

        DxDataGrid<Tmrbr001ConfirmPlanResultDto> grid;

        IQueryable<Tmrbr001ConfirmPlanResultDto> DataSource;

        IEnumerable<object> SelectedKeyIds;

        int SelectedCount { get; set; }

        int StoredSelectedCount { get; set; }

        int StoredUnselectedCount { get; set; }

        string WorkflowId { get; set; }

        string Message { get; set; }


        protected override async Task OnInitializedAsync()
        {
            GetData();
        }

        protected override Task OnAfterRenderAsync(bool firstRender)
        {
            return base.OnAfterRenderAsync(firstRender);
        }

        protected async Task OnSelectionChanged(DataGridSelection<Tmrbr001ConfirmPlanResultDto> selection)
        {
            StoredSelectedCount = selection.SelectedKeysStored.Count();
            StoredUnselectedCount = selection.UnselectedKeysStored.Count();
            var selectedKeys = await selection.SelectedKeys;

            SelectedKeyIds = selectedKeys;

            SelectedCount = selectedKeys.Count();
            await InvokeAsync(StateHasChanged);
        }

        void Query(MouseEventArgs args)
        {
            GetData();
        }

        void GetData()
        {
            var querydto = new QueryTmrbr001InputDto()
            {
                FacNo = starship.FacNo,
                OrderNo = starship.OrderNo,
                StockNo = starship.StockNo,
                MatNo = starship.MatNo,
                DeliverDate1 = starship.DeliverDate1.ToString(),
                DeliverDate2 = starship.DeliverDate2.ToString()
            };
            DataSource = ConfmPlanAppService.GetTmr01_ConfirmPlan(querydto);
        }

        void CreatePopl001(MouseEventArgs args)
        {
            List<Guid> guids = new List<Guid>();
            foreach (var id in SelectedKeyIds)
            {
                guids.Add(Guid.Parse(id.ToString()));
            }
            List<ConfmMatDto> matDtos = new List<ConfmMatDto>();
            var datas = DataSource.Where(x => guids.Contains(x.Id)).ToList();
            foreach (var data in datas)
            {
                matDtos.Add(new ConfmMatDto()
                {
                    MatNo = data.MatNo,
                    MatLen = data.MatLen,
                    MatThick = data.MatThick,
                    MatWidth = data.MatWidth,
                    MatWt = data.MatWt,
                    OrderNo = data.OrderNo,
                    NextWholeBacklogSeq = data.NextWholeBacklogSeq,
                    WholeBacklogNo = data.WholeBacklogNo
                });
            }
            WorkflowId = ConfmPlanAppService.Popl001_insert(matDtos);

            //Thread.Sleep(1000);

            var result = ConfmPlanAppService.GetPopl001(WorkflowId);
            if (result.WorkflowStatus == 2 && result.Code == 0)
            {
                GetData();
            }
            else if (result.WorkflowStatus == 2 && result.Code == 1)
            {
                Message = result.Message;
            }

        }

    }
